package com.openbravo.beans;

import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.util.Date;

import javax.swing.BoxLayout;

import com.openbravo.data.basic.BasicException;
import com.openbravo.format.Formats;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;

public class JStartEndDateSelectPanel extends JPanel {
	private JTextField jTxtStartDate;
	private JTextField jTxtEndDate;

	/**
	 * Create the panel.
	 */
	public JStartEndDateSelectPanel() {
		setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
		
		JLabel label = new JLabel("时间启");
		add(label);
		
		jTxtStartDate = new JTextField();
		jTxtStartDate.setColumns(10);
		add(jTxtStartDate);
		
		JButton btnStartTime = new JButton("");
		btnStartTime.setIcon(new ImageIcon(JStartEndDateSelectPanel.class.getResource("/com/openbravo/images/date.png")));
		btnStartTime.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnDateStartActionPerformed(e);
			}
		});
		add(btnStartTime);
		
		JLabel label_1 = new JLabel("时间止");
		add(label_1);
		
		jTxtEndDate = new JTextField();
		jTxtEndDate.setColumns(10);
		add(jTxtEndDate);
		
		JButton btnEndTime = new JButton("");
		btnEndTime.setIcon(new ImageIcon(JStartEndDateSelectPanel.class.getResource("/com/openbravo/images/date.png")));
		btnEndTime.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnDateEndActionPerformed(e);
			}
		});
		add(btnEndTime);
	}
	
	private void btnDateStartActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_btnDateStartActionPerformed
		Date date;
		try {
			date = (Date) Formats.TIMESTAMP.parseValue(jTxtStartDate.getText());
		} catch (BasicException e) {
			date = null;
		}
		date = JCalendarDialog.showCalendarTimeHours(this, date);
		if (date != null) {
			jTxtStartDate.setText(Formats.TIMESTAMP.formatValue(date));
		}
	}// GEN-LAST:event_btnDateStartActionPerformed

	private void btnDateEndActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_btnDateEndActionPerformed
		Date date;
		try {
			date = (Date) Formats.TIMESTAMP.parseValue(jTxtEndDate.getText());
		} catch (BasicException e) {
			date = null;
		}
		date = JCalendarDialog.showCalendarTimeHours(this, date);
		if (date != null) {
			jTxtEndDate.setText(Formats.TIMESTAMP.formatValue(date));
		}
	}// GEN-LAST:event_btnDateEndActionPerformed

	
	public Date getStartDate(){
		Date date;
		try {
			date = (Date) Formats.TIMESTAMP.parseValue(jTxtStartDate.getText());
		} catch (BasicException e) {
			date = null;
		}
		return date;
	}
	
	public Date getEndDate(){
		Date date;
		try {
			date = (Date) Formats.TIMESTAMP.parseValue(jTxtEndDate.getText());
		} catch (BasicException e) {
			date = null;
		}
		return date;
	}
}
